package com.controller;


import com.business.hexinpass.util.Validator;
import com.entity.EasyUITree;
import com.entity.Menu;
import com.entity.Operator;
import com.service.MenuService;
import com.service.OperatorService;
import com.vo.JsonReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by jsb-160 on 2018/7/18.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    //得到菜单列表，包括子菜单
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn getMenulistByEasyUI(HttpSession session) {
        JsonReturn jsonReturn = new JsonReturn();
//        初始化Id
//        userId = 1;
        Operator operator= (Operator) session.getAttribute("logined");
        if(null==operator||Validator.isNull(String.valueOf(operator.getId()))){
            jsonReturn.setError("请重新登录");
            jsonReturn.setSuccess(false);
        }
//        查到主菜单
        List<Menu> menuList = menuService.liseMenu(operator.getId());
        jsonReturn.setData(menuToEasyUI(menuList));
        return jsonReturn;

    }

    /**
     * 得到菜单列表，包括子菜单(以menu返回)
     * @param
     * @return
     */
    @RequestMapping(value = "/listMenu", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn getMenulist() {
//        初始化Id
//        userId = 1;
        JsonReturn jsonReturn = new JsonReturn();
//        查到主菜单
        List<Menu> menuList = menuService.selectListMenu();
        jsonReturn.setData(menuList);
        return jsonReturn;

    }

    //    通过pid查询子菜单列表
    @RequestMapping(value = "/getChildMenu")
    @ResponseBody
    public JsonReturn getChildMenuById(Integer pid, HttpSession session) {
        JsonReturn jsonReturn = new JsonReturn();
        Operator operator= (Operator) session.getAttribute("logined");
        if(null==operator||Validator.isNull(String.valueOf(operator.getId()))){
            jsonReturn.setError("请重新登录");
            jsonReturn.setSuccess(false);
        }
        List<EasyUITree> easyUITrees = menuToEasyUI(menuService.liseMenuByPid(pid, operator.getId()));
        jsonReturn.setData(easyUITrees);
        return jsonReturn;
    }
 //    通过pid查询子菜单列表(以menu返回)
    @RequestMapping(value = "/getMenuByPid")
    @ResponseBody
    public JsonReturn getChildMenu(Integer pid) {
        JsonReturn jsonReturn = new JsonReturn();
//        userId = 1;
        List<Menu> menus=menuService.selectListMenuByPid(pid);
        jsonReturn.setData(menus);
        return jsonReturn;
    }

    /**
     * 查询菜单详情
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/getMenuInfo")
    @ResponseBody
    public JsonReturn selectMenu(Integer menuId) {
        JsonReturn jsonReturn = new JsonReturn();
        try {
            Menu menu= menuService.selectMenuById(menuId);
            jsonReturn.setData(menu);
            jsonReturn.setSuccess(true);
        } catch (Exception e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setError("查询失败");
            e.printStackTrace();
        }
        return jsonReturn;
    }

    /**
     * 添加或修改菜单属性
     *
     * @param menu
     * @return
     */
    @RequestMapping(value = "/addOrEditMenu")
    @ResponseBody
    public JsonReturn addOrEditMenu(Menu menu) {
        JsonReturn jsonReturn = new JsonReturn();
        //修改菜单属性
        try {
            if (Validator.isNotNull(String.valueOf(menu.getId()))) {
                menuService.updateMenu(menu);
                jsonReturn.setData("修改成功");
                jsonReturn.setSuccess(true);
            } else {
                menuService.addMenu(menu);
                jsonReturn.setData("添加成功");
                jsonReturn.setSuccess(true);
            }
        } catch (Exception e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setError("系统错误");
            e.printStackTrace();
        }
        return jsonReturn;
    }

    //将菜单的list转到easyUI树
    private List<EasyUITree> menuToEasyUI(List<Menu> menuList) {
        List<EasyUITree> easyUITrees = new ArrayList<EasyUITree>();
        for (Menu menu : menuList) {
            EasyUITree easyUITree = new EasyUITree();
            easyUITree.setId(menu.getId());
            easyUITree.setPid(menu.getPid());
            easyUITree.setText(menu.getName());
            easyUITree.setState("closed");
            easyUITree.setAttributes(menu.getTargetUrl());
            easyUITrees.add(easyUITree);
        }
        return easyUITrees;
    }
}
